package com.hspedu.tankgame3;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Iterator;
import java.util.Vector;

//为了让Panel不停的重绘子弹,需要将MyPanel实现Runnable,当做一个线程使用
public class MyPanel extends JPanel implements KeyListener,Runnable {

    private Hero hero = null;

    private Vector<EnemyTank> enemyTanks=new Vector<>();

    private int enemyTankSize=3;

    public MyPanel() {

        hero = new Hero(100, 100);
        hero.setSpeed(5);

        //初始化敌人tank
        for(int i = 0;i < enemyTankSize; i++){
            EnemyTank enemyTank = new EnemyTank((i+1)*100,0);
            enemyTank.setDirect(2);
            enemyTanks.add(enemyTank);
        }

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //默认填充颜色为黑色
        g.fillRect(0, 0, 1000, 750);
        //draw Tank
        drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 1);

        //画出射击的子弹
        if(hero.shot != null && hero.shot.isLive==true){
            System.out.println("子弹被绘制!");
            g.draw3DRect(hero.shot.x,hero.shot.y,1,1,false);
        }

       /*Iterator<EnemyTank> list =enemyTanks.iterator();

        while (list.hasNext()) {
            EnemyTank enemyTank =  list.next();
            drawTank(enemyTank.getX(),enemyTank.getY(),g,enemyTank.getDirect(),0);
        }*/

        enemyTanks.forEach(enemyTank->{
            drawTank(enemyTank.getX(),enemyTank.getY(),g,enemyTank.getDirect(),0);
        });


    }

    /**
     * @param x      左上角x坐标
     * @param y      左上角y坐标
     * @param g      画笔
     * @param direct tank方向(上、下、左、右)
     * @param type   tank类型
     */
    public void drawTank(int x, int y, Graphics g, int direct, int type) {

        switch (type) {
            case 0:  //敌人的tank
                g.setColor(Color.CYAN);
                break;
            case 1:  //我们的tank
                g.setColor(Color.YELLOW);
                break;
        }

        //direct 表示方向:(0:向上,1:向右,2:向下,3:向左)
        switch (direct) {
            case 0: //表示向上
                g.fill3DRect(x, y, 10, 60, false);//左轮子
                g.fill3DRect(x + 30, y, 10, 60, false);//右轮子
                g.fill3DRect(x + 10, y + 10, 20, 40, false); //tank 盖子
                g.fillOval(x + 10, y + 20, 20, 20); //圆形盖子
                g.drawLine(x + 20, y + 30, x + 20, y);//炮筒
                break;
            case 1: //表示向右
                g.fill3DRect(x, y, 60, 10, false);//上边的轮子
                g.fill3DRect(x, y + 30, 60, 10, false);//下边的轮子
                g.fill3DRect(x + 10, y + 10, 40, 20, false); //tank 盖子
                g.fillOval(x + 20, y + 10, 20, 20); //圆形盖子
                g.drawLine(x + 30, y + 20, x + 60, y + 20);//炮筒
                break;
            case 2: //表示向下
                g.fill3DRect(x, y, 10, 60, false);//左轮子
                g.fill3DRect(x + 30, y, 10, 60, false);//右轮子
                g.fill3DRect(x + 10, y + 10, 20, 40, false); //tank 盖子
                g.fillOval(x + 10, y + 20, 20, 20); //圆形盖子
                g.drawLine(x + 20, y + 30, x + 20, y + 60);//炮筒
                break;
            case 3: //表示向左
                g.fill3DRect(x, y, 60, 10, false);//上边的轮子
                g.fill3DRect(x, y + 30, 60, 10, false);//下边的轮子
                g.fill3DRect(x + 10, y + 10, 40, 20, false); //tank 盖子
                g.fillOval(x + 20, y + 10, 20, 20); //圆形盖子
                g.drawLine(x + 30, y + 20, x, y + 20);//炮筒
                break;
            default:
                System.out.println("暂时没有处理");
        }

    }


    @Override
    public void keyTyped(KeyEvent e) {

    }

    //处理 WDSA 键按下的情况
    @Override
    public void keyPressed(KeyEvent e) {

        if(e.getKeyCode() == KeyEvent.VK_W){
            //改变Tank的方向
            hero.setDirect(0);
            hero.moveUp();
        }else if(e.getKeyCode() == KeyEvent.VK_D){
            hero.setDirect(1);
            hero.moveRight();
        }else if(e.getKeyCode() == KeyEvent.VK_S){
            hero.setDirect(2);
            hero.moveDown();
        }else if(e.getKeyCode()==KeyEvent.VK_A){
            hero.setDirect(3);
            hero.moveLeft();
        }else if(e.getKeyCode()==KeyEvent.VK_J){
            System.out.println("按下了J键,开始射击.");
            hero.shotEnemyTank();
        }

        this.repaint();//让面板重绘,会再次调用paint方法
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() { //每隔 100毫秒，重绘区域, 刷新绘图区域, 子弹就移动
        while (true){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.repaint();
        }
    }
}



















